package com.diaohaiwen.basic.websockt.service;

import com.diaohaiwen.basic.websockt.StorageUtil;
import com.diaohaiwen.basic.websockt.mapper.NoticeMapper;
import com.diaohaiwen.basic.websockt.obj.bo.FileBo;
import com.diaohaiwen.basic.websockt.obj.dto.FileDto;
import com.diaohaiwen.basic.websockt.obj.dto.TextDto;
import com.diaohaiwen.basic.websockt.obj.model.Notice;
import com.diaohaiwen.basic.websockt.obj.vo.NoticeVo;
import com.diaohaiwen.common.enums.NoticeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 刁海文
 * @since 2023/12/7 22:43
 */
@Service
public class NoticeService {
    @Resource
    NoticeMapper noticeMapper;

    public NoticeVo text(TextDto text) {

        Notice notice = Notice.build();
        notice.setContents(text.getContents())
                .setReceiverId(text.getReceiverId())
                .setReceiverName(text.getReceiverName())
                .setGroup(text.getGroup())
                .setNoticeType(NoticeEnum.TEXT);
        noticeMapper.insert(notice);

        return notice.toVO();
    }

    public NoticeVo file(FileDto receiver, MultipartFile file) {

        FileBo storage = StorageUtil.storage(file);
        Notice notice = Notice.build();

        notice.setContents(storage.toString())
                .setReceiverId(receiver.getReceiverId())
                .setReceiverName(receiver.getReceiverName())
                .setGroup(receiver.getGroup())
                .setNoticeType(NoticeEnum.FILE);

        return notice.toVO();
    }
}
